package com.somiz.structure;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import org.hibernate.Session;

import com.somiz.main.Control;
import com.tech.hibernate.HibernateUtil;

public class StructureForm extends JPanel {
	private static Long structureId;
	private JTextField T_code;
	private JTextField T_nom;
	private JTextField T_tiers;

	public StructureForm() {
		setLayout(null);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setBorder(new TitledBorder(UIManager
				.getBorder("TitledBorder.border"), "Structure :",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 101, 393, 163);
		add(panel);

		JLabel lblNomDeLa = new JLabel("Code de la structure : ");
		lblNomDeLa.setBounds(29, 26, 124, 28);
		panel.add(lblNomDeLa);

		T_code = new JTextField();
		T_code.setColumns(10);
		T_code.setBounds(163, 23, 220, 34);
		panel.add(T_code);

		T_nom = new JTextField();
		T_nom.setColumns(10);
		T_nom.setBounds(163, 65, 220, 34);
		panel.add(T_nom);

		JLabel lblNomDeLa_1 = new JLabel("Nom de la structure :");
		lblNomDeLa_1.setBounds(29, 68, 124, 28);
		panel.add(lblNomDeLa_1);

		T_tiers = new JTextField();
		T_tiers.setColumns(10);
		T_tiers.setBounds(163, 110, 220, 34);
		panel.add(T_tiers);

		JLabel lblTiresDeLa = new JLabel("Tires de la structure :");
		lblTiresDeLa.setBounds(29, 113, 124, 28);
		panel.add(lblTiresDeLa);

		JButton button_1 = new JButton("");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (errForm().equals("")) {
					if (structureId == null) {
						// creer
						Structure structure = new Structure();
						structure = FormVersStructure(structure);
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						if (Control.getStructureDao().isExisteAvecCode(
								structure, session)) {
							JOptionPane
									.showMessageDialog(
											null,
											"cette structure existe deja dans votre base de donnees",
											"Erreur",
											JOptionPane.WARNING_MESSAGE);
						} else {
							Control.getStructureDao().Creer(structure, session);
							structureId = structure.getId();
						}
						session.close();
					} else {
						// modif
						Session session = HibernateUtil.getSessionFactory()
								.openSession();
						Structure structure = new Structure();
						structure = FormVersStructure(structure);
						if (Control.getStructureDao().isExisteAvecCode(
								structure, session)) {
							if (structureId.equals(Control.getStructureDao()
									.TrouverAvecCode(structure, session)
									.getId())) {
								structure = Control.getStructureDao()
										.TrouverAvecId(structureId, structure,
												session);
								structure = FormVersStructure(structure);
								Control.getStructureDao().modifier(structure,
										session);
							} else {
								JOptionPane
										.showMessageDialog(
												null,
												"cette structure existe deja dans votre base de donnees",
												"Erreur",
												JOptionPane.WARNING_MESSAGE);
							}
						} else {
							structure = Control.getStructureDao()
									.TrouverAvecId(structureId, structure,
											session);
							structure = FormVersStructure(structure);
							Control.getStructureDao().modifier(structure,
									session);
						}
						session.close();
					}
				} else {
					JOptionPane.showMessageDialog(null, errForm(), "Erreur",
							JOptionPane.WARNING_MESSAGE);
				}
			}
		});
		button_1.setIcon(new ImageIcon(StructureForm.class
				.getResource("/cc/black/png/save_icon&32.png")));
		button_1.setPreferredSize(new Dimension(50, 50));
		button_1.setMnemonic(KeyEvent.VK_ENTER);
		button_1.setBackground(Color.WHITE);
		button_1.setBounds(10, 11, 50, 50);
		add(button_1);

		JButton button_2 = new JButton("");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vider();
			}
		});
		button_2.setIcon(new ImageIcon(StructureForm.class
				.getResource("/cc/black/png/delete_icon&32.png")));
		button_2.setPreferredSize(new Dimension(50, 50));
		button_2.setMnemonic(KeyEvent.VK_ENTER);
		button_2.setBackground(Color.WHITE);
		button_2.setBounds(70, 11, 50, 50);
		add(button_2);

		JSeparator separator = new JSeparator();
		separator.setBounds(10, 81, 393, 2);
		add(separator);

		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				vider();
				structureId = null;
			}
		});
		button.setIcon(new ImageIcon(StructureForm.class
				.getResource("/images/png/Add32.png")));
		button.setPreferredSize(new Dimension(50, 50));
		button.setMnemonic(KeyEvent.VK_ENTER);
		button.setBackground(Color.WHITE);
		button.setBounds(130, 11, 50, 50);
		add(button);

	}

	public void StructureVersForm(Structure structure) {
		T_nom.setText(structure.getDesig());
		T_code.setText(structure.getCode());
		T_tiers.setText(structure.getTier());
	}

	public Structure FormVersStructure(Structure structure) {
		structure.setDesig(T_nom.getText());
		structure.setCode(T_code.getText());
		structure.setTier(T_tiers.getText());

		return structure;
	}

	public String errForm() {
		String err = "";
		if (T_code.getText().equals("")) {
			err = err + "il faut donner un code pour la Structure\n";
		}
		return err;
	}

	public void vider() {
		T_nom.setText("");
		T_code.setText("");
		T_tiers.setText("");
		structureId = null;
	}

	public static final Long getStructureId() {
		return structureId;
	}

	public static final void setStructureId(Long structureId) {
		StructureForm.structureId = structureId;
	}

}
